import Vue from 'vue'
import Router from 'vue-router'
import App from '../App'
import store from '../store/'
import { NEXTPAGE } from '../store/mutation-types'


const home = r => require.ensure([], () => r(require('../page/home/Home')), 'home')
const create_post = r => require.ensure([], () => r(require('../page/home/CreatePost')), 'create_post')
const article = r => require.ensure([], () => r(require('../page/home/Article')), 'article')
const comment = r => require.ensure([], () => r(require('../page/home/Comment')), 'comment')
const comment2 = r => require.ensure([], () => r(require('../page/home/Comment2')), 'comment2')
const simple_share = r => require.ensure([], () => r(require('../page/home/SimpleShare.vue')), 'simple_share')
const room_index = r => require.ensure([], () => r(require('../page/room/RoomIndex')), 'room_index')
const room_info = r => require.ensure([], () => r(require('../page/room/RoomInfo')), 'room_info')
const search_room = r => require.ensure([], () => r(require('../page/room/SearchRoom.vue')), 'search_room')
const room = r => require.ensure([], () => r(require('../page/room/Room')), 'room')
const photo = r => require.ensure([], () => r(require('../page/room/Photo')), 'photo')
const create_room = r => require.ensure([], () => r(require('../page/room/CreateRoom')), 'create_room')
const create_album = r => require.ensure([], () => r(require('../page/room/CreateAlbum')), 'create_album')
const application = r => require.ensure([], () => r(require('../page/application/Application')), 'application')

const profile = r => require.ensure([], () => r(require('../page/profile/Profile')), 'profile')
const user_info = r => require.ensure([], () => r(require('../page/profile/UserInfo')), 'user_info')

const login = r => require.ensure([], () => r(require('../page/account/Login')), 'login')
const register = r => require.ensure([], () => r(require('../page/account/Register')), 'register')
const reset_password01 = r => require.ensure([], () => r(require('../page/account/ResetPasseord01')), 'reset_password01')
const reset_password02 = r => require.ensure([], () => r(require('../page/account/ResetPasseord02')), 'reset_password02')

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      component: App,
      children: [
        //地址为空时跳转home页面
        {
          path: '',
          redirect: '/home',
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/home',
          meta: {
            requireAuth: true,
          },
          component: home
        },
        {
          path: '/create_post',
          meta: {
            requireAuth: true,
          },
          component: create_post
        },
        {
          path: '/article',
          meta: {
            requireAuth: true,
          },
          component: article
        },

        {
          path: '/room_index',
          meta: {
            requireAuth: true,
          },
          component: room_index
        },

        {
          path: '/room_info',
          meta: {
            requireAuth: true,
          },
          component: room_info
        },

        {
          path: '/search_room',
          meta: {
            requireAuth: true,
          },
          component: search_room
        },
        {
          path: '/comment',
          meta: {
            requireAuth: true,
          },
          component: comment
        },
        {
          path: '/comment2',
          meta: {
            requireAuth: true,
          },
          component: comment2
        },
        {
          path: '/simple_share',
          meta: {
            requireAuth: true,
          },
          component: simple_share
        },
        {
          path: '/room',
          meta: {
            requireAuth: true,
          },
          component: room
        },
        {
          path: '/photo',
          meta: {
            requireAuth: true,
          },
          component: photo
        },
        {
          path: '/create_room',
          meta: {
            requireAuth: true,
          },
          component: create_room
        },
        {
          path: '/create_album',
          meta: {
            requireAuth: true,
          },
          component: create_album
        },

        {
          path: '/application',
          meta: {
            requireAuth: true,
          },
          component: application
        },

        {
          path: '/profile',
          meta: {
            requireAuth: true,
          },
          component: profile
        },
        {
          path: '/user_info',
          meta: {
            requireAuth: true,
          },
          component: user_info
        },

        {
          path: '/login',
          meta: {
            requireAuth: false,
          },
          component: login
        },
        {
          path: '/register',
          meta: {
            requireAuth: false,
          },
          component: register
        },
        {
          path: '/reset_password01',
          meta: {
            requireAuth: false,
          },
          component: reset_password01
        },
        {
          path: '/reset_password02',
          meta: {
            requireAuth: false,
          },
          component: reset_password02
        },
      ]
    }
  ]
});



// 页面刷新时，重新赋值token
if (window.localStorage.getItem('token')) {
  store.commit(NEXTPAGE, window.localStorage)
}

router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requireAuth)) {
    if (store.state.token) {
      next();
    }
    else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  }
  else {
    next();
  }
})


export default router
